package com.hurl.library.spider;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hurl.library.http.HttpException;
import com.hurl.library.http.HttpRequest;

public class Spider {
	private static final Logger log = Logger.getLogger(Spider.class);
	private SpiderConfig config;
	List<SpiderListener> listenerList =  new ArrayList<SpiderListener>();
	HttpRequest request = new HttpRequest();
	
	public void start() throws SpiderException{
		/*
		 * 检查配置项是否正确配置
		 */
		if(config==null){
			throw new SpiderException("没有配置项");
		}
		UrlPattern pattern = config.getUrlPattern();
		if(pattern==null){
			throw new SpiderException("没有指定网址");
		}

		/*
		 * 通知监听器下载即将开始
		 */
		if(!listenerList.isEmpty()){
			for(SpiderListener listener : listenerList){
				listener.startup();
			}
		}
		Throwable t = null;
		/** 开始下载 */
		boolean success = true;
		try{
			if(pattern instanceof SimpleUrlPattern){
				requestUrl(((SimpleUrlPattern)pattern).getUrl());
			}else if(pattern instanceof MultiUrlPattern){
				MultiUrlPattern turl = (MultiUrlPattern)pattern;
				String url = null;
				while((url=turl.nextUrl())!=null){
					requestUrl(url);
				}
			}else{
				throw new SpiderException("不支持的网址规则类型："+pattern.getClass().getName());
			}
		}catch(Throwable e){
			t = e;
			success = false;
		}
		
		/** 通知监听器下载已结束 */
		if(!listenerList.isEmpty()){
			for(SpiderListener listener : listenerList){
				listener.teardown(success,t);
			}
		}
		

	}
	private void requestUrl(String url)throws SpiderException{

		//过滤网址
		if(config.getUrlFilter()!=null && !config.getUrlFilter().filter(url)){
			log.warn("网址过滤不通过："+url);
			return;
		}
		
		if(!listenerList.isEmpty()){
			for(SpiderListener listener : listenerList){
				listener.beforeDownload(url);
			}
		}
		
		try {
			if(!SpiderConfig.HTTP_METHOD_GET.equalsIgnoreCase(config.getHttpMethod())){
				throw new SpiderException("不支持的请求类型："+SpiderConfig.HTTP_METHOD_POST);
			}
			
			String html = request.request(url);
			
			if(!listenerList.isEmpty()){
				for(SpiderListener listener : listenerList){
					listener.downloadSuccess(url, html);
				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
			
			if(!listenerList.isEmpty()){
				for(SpiderListener listener : listenerList){
					listener.downloadFailure(url,e);
				}
			}
		}
	}
	public void setConfig(SpiderConfig config){
		this.config=config;
	}
	
	public void addListener(SpiderListener listener){
		listenerList.add(listener);
	}
	public void removeListener(SpiderListener listener){
		listenerList.remove(listener);
	}
	public void removeAllListener(){
		listenerList.clear();
	}
}
